package com.ruoyi.ma.listener;

import cn.dev33.satoken.secure.BCrypt;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.CellExtra;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.excel.ExcelListener;
import com.ruoyi.common.excel.ExcelResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.ValidatorUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.ma.domain.MaMaterialManifest;
import com.ruoyi.ma.domain.MaMaterialSupplier;
import com.ruoyi.ma.domain.vo.MaMaterialManifestVo;
import com.ruoyi.ma.domain.vo.MaMaterialSupplierVo;
import com.ruoyi.ma.mapper.MaMaterialManifestMapper;
import com.ruoyi.ma.mapper.MaMaterialSupplierMapper;
import com.ruoyi.ma.service.IMaMaterialManifestService;
import com.ruoyi.ma.service.IMaMaterialSupplierService;
import com.ruoyi.system.domain.vo.SysUserImportVo;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;

import java.util.List;

/**
 * 材料清单导入
 *
 */
@Slf4j
public class MaMaterialfestImportListener extends AnalysisEventListener<MaMaterialManifestVo> implements ExcelListener<MaMaterialManifestVo> {

    private final IMaMaterialSupplierService supplierService;
    private final IMaMaterialManifestService manifestService;
    private final MaMaterialManifestMapper manifestMapper;

    private final MaMaterialSupplierVo supplier;

    private int successNum = 0;
    private int failureNum = 0;
    private final StringBuilder successMsg = new StringBuilder();
    private final StringBuilder failureMsg = new StringBuilder();

    public MaMaterialfestImportListener(Long gysId) {
        this.supplierService = SpringUtils.getBean(IMaMaterialSupplierService.class);
        this.manifestService = SpringUtils.getBean(IMaMaterialManifestService.class);
        this.manifestMapper = SpringUtils.getBean(MaMaterialManifestMapper.class);

        LambdaQueryWrapper<MaMaterialSupplier> lqw = Wrappers.lambdaQuery();
        lqw.eq(gysId != null, MaMaterialSupplier::getSupplierId, gysId);
        supplier = supplierService.queryById(gysId);
    }

    @Override
    public void onException(Exception exception, AnalysisContext context) throws Exception {
        super.onException(exception, context);
    }

    @Override
    public void invoke(MaMaterialManifestVo vo, AnalysisContext analysisContext) {
        MaMaterialManifest bean = BeanUtil.toBean(vo, MaMaterialManifest.class);
        try {
            bean.setBdId(supplier.getBdId());
            bean.setZdId(supplier.getZdId());
            bean.setGysId(supplier.getSupplierId());
            ValidatorUtils.validate(bean);
            manifestMapper.insert(bean);
            successNum++;
            successMsg.append("<br/>").append(successNum).append("、清单名称 ").append(bean.getManifestName()).append(" 导入成功");
        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum + "、清单名称 " + bean.getManifestName() + " 导入失败：";
            failureMsg.append(msg).append(e.getMessage());
            log.error(msg, e);
        }
    }

    @Override
    public void extra(CellExtra extra, AnalysisContext context) {
        super.extra(extra, context);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }

    @Override
    public boolean hasNext(AnalysisContext context) {
        return super.hasNext(context);
    }

    @Override
    public ExcelResult<MaMaterialManifestVo> getExcelResult() {
        return new ExcelResult<MaMaterialManifestVo>() {

            @Override
            public String getAnalysis() {
                if (failureNum > 0) {
                    failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
                    throw new ServiceException(failureMsg.toString());
                } else {
                    successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
                }
                return successMsg.toString();
            }

            @Override
            public List<MaMaterialManifestVo> getList() {
                return null;
            }

            @Override
            public List<String> getErrorList() {
                return null;
            }
        };
    }
}
